package com.zp.business.controller.patient;

import com.zp.business.entity.patient.bo.ClinicMasterBo;
import com.zp.business.entity.patient.vo.ClinicMasterVo;
import com.zp.business.service.patient.IClinicMasterService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 门诊就诊记录控制器
 * 前端访问路由地址为:/business/clinicMaster
 *
 * @author zhang peng
 * @ date 2023-12-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/clinicMaster")
public class ClinicMasterController extends BaseController {

    private final IClinicMasterService iClinicMasterService;

    /**
     * 查询门诊就诊记录列表 (分页)
     */
    @GetMapping("/selectPage")
    public TableDataInfo<ClinicMasterVo> selectPage (ClinicMasterBo bo, PageQuery pageQuery) {
        return iClinicMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询门诊就诊记录列表 (不分页)
     */
    @GetMapping("/list")
    public R<List<ClinicMasterVo>> list (ClinicMasterBo bo) {
        List<ClinicMasterVo> list = iClinicMasterService.queryList(bo);
        return R.ok(list);
    }

    @GetMapping("getPatientCount")
    public R<Map<String, Integer>> getPatientCount (ClinicMasterBo bo) {
        return R.ok(iClinicMasterService.getPatientCount(bo));
    }

    /**
     * 获取已诊、未诊、待诊人数 返回map
     * @param bo
     * @return map
     */
    @GetMapping("getNumGroupClinicType")
    public R<Map<String, Integer>> getNumGroupClinicType (ClinicMasterBo bo) {
        return R.ok(iClinicMasterService.getNumGroupClinicType(bo));
    }

    /**
     * 导出门诊就诊记录列表
     */
    @Log(title = "门诊就诊记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (ClinicMasterBo bo, HttpServletResponse response) {
        List<ClinicMasterVo> list = iClinicMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "门诊就诊记录", ClinicMasterVo.class, response);
    }

    /**
     * 获取门诊就诊记录详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ClinicMasterVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iClinicMasterService.queryById(id));
    }

    /**
     * 新增门诊就诊记录
     */
    @Log(title = "门诊就诊记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody ClinicMasterBo bo) {
        return toAjax(iClinicMasterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改门诊就诊记录
     */
    @Log(title = "门诊就诊记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody ClinicMasterBo bo) {
        return toAjax(iClinicMasterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除门诊就诊记录
     *
     * @param ids 主键串
     */
    @Log(title = "门诊就诊记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iClinicMasterService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
